package com.lingdian.resume.controller;

import java.io.File;
import java.util.Iterator;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lingdian.common.util.MhtUtil;
import com.lingdian.common.vo.JSONResult;
import com.lingdian.resume.bean.ResumeInfo;
import com.lingdian.resume.repository.ResumeInfoRepository;
import com.lingdian.resume.service.LuceneResumeInfoService;
import com.lingdian.resume.service.ResumeInfoService;

@Controller
@RequestMapping("/resume/")
public class ResumeInfoController {
	
	@Autowired
	private ResumeInfoService resumeInfoService;
	@Autowired
	private ResumeInfoRepository resumeInfoRepository;
	
	@Autowired
	private LuceneResumeInfoService luceneResumeInfoService;

	@RequestMapping("/updateAll")
	public void updateAll(){
		File dir = new File("E:\\jianli");
		File[] files = dir.listFiles();
		int htmlCount = 0;
		for(File file:files){
			if(file.getName().endsWith("html")){
				htmlCount++;
			}else if(file.getName().endsWith(".mht")){
				String html = MhtUtil.mht2html(file.getPath());
				if(html!=null){
					Document dom = Jsoup.parse(html);
					String info = dom.body().text();
					resumeInfoService.saveResumeInfoFromHtml(info, file.getName());
				}
			}
		}
	}
	@RequestMapping("/createIndex")
	public @ResponseBody JSONResult createIndex(){
		Iterator<ResumeInfo> iterators = resumeInfoRepository.findAll().iterator();
		luceneResumeInfoService.createIndex(iterators);
		return new JSONResult(0, "");
	}
	@RequestMapping("/queryText")
	public @ResponseBody List<ResumeInfo> query(String content){
		return luceneResumeInfoService.queryText(content);
	}
}
